<template>
  <div class="ele-body">
    <el-card shadow="never">
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="4" :md="12">
            <el-form-item label="名称:">
              <el-input
                :placeholder="'请输入名称'"
                v-model="where.name"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid"
              >
                <el-option label="全部" value="" />
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="投放设备:">
              <el-select
                clearable
                v-model="where.device"
                placeholder="请选择投放设备"
                class="ele-fluid"
              >
                <!-- 1=全部 , 2=安卓, 3=IOS, 4=PAD,  5=IPAD, 6=PC', -->
                <el-option label="全部" value="1" />
                <el-option label="安卓" value="2" />
                <el-option label="IOS" value="3" />
                <el-option label="PAD" value="4" />
                <el-option label="IPAD" value="5" />
                <el-option label="PC" value="6" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:game-rank:add')"
            >添加
          </el-button>
          <!-- <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch"
            v-if="permission.includes('sys:box-yun-course:delete')">删除
          </el-button> -->
        </template>
        <!-- 操作列 -->

        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-setting"
            @click="openPindao(row)"
            v-if="permission.includes('sys:game-rank:gameview')"
            >榜单游戏
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:game-rank:edit')"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此榜单吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:game-rank:delete')"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="2"
          />
        </template>

        <!-- 投放设备列 -->
        <template slot="device" slot-scope="{ row }">
          <span
            v-for="(value, index) in row.device.split(',')"
            :key="index"
            style="padding-left: 8px"
          >
            <el-tag size="mini" v-if="value == ''">暂无</el-tag>
            <el-tag
              size="mini"
              v-else
              :type="
                ['success', 'primary', 'warning', 'info', 'info', 'danger'][
                  index - 1
                ]
              "
              >{{
                ["全部", "安卓", "IOS", "PAD", "IPAD", "PC"][value - 1]
              }}</el-tag
            >
          </span>
        </template>
        <!--        <template slot="scene" slot-scope="{ row }">-->
        <!--          <el-button size="mini" v-if="row.scene === 1" type="success"-->
        <!--            >三端互通</el-button-->
        <!--          >-->
        <!--          <el-button size="mini" v-if="row.scene === 2" type="primary"-->
        <!--            >纯PC展示</el-button-->
        <!--          >-->
        <!--          <el-button size="mini" v-if="row.scene === 3" type="danger"-->
        <!--            >热搜管理</el-button-->
        <!--          >-->
        <!--        </template>-->

        <!-- icon列 -->
        <!--        <template slot="icon" slot-scope="{ row }">-->
        <!--          <el-image v-if="row.icon == ''" style="width: 30px; height: 30px">-->
        <!--            <div slot="error" class="image-slot">-->
        <!--              <i class="el-icon-picture-outline"></i>-->
        <!--            </div>-->
        <!--          </el-image>-->
        <!--          <el-image-->
        <!--            v-else-->
        <!--            style="width: 30px; height: 30px"-->
        <!--            :src="row.icon"-->
        <!--            :preview-src-list="[row.icon]"-->
        <!--            fit="fit"-->
        <!--          ></el-image>-->
        <!--        </template>-->

        <!--        <template slot="icon" slot-scope="{ row }">-->
        <!--          <el-image v-if="row.icon == ''" style="width: 30px; height: 30px">-->
        <!--            <div slot="error" class="image-slot">-->
        <!--              <i class="el-icon-picture-outline"></i>-->
        <!--            </div>-->
        <!--          </el-image>-->
        <!--          <el-image-->
        <!--            v-else-->
        <!--            style="width: 30px; height: 30px"-->
        <!--            :src="row.icon"-->
        <!--            :preview-src-list="[row.icon]"-->
        <!--            fit="fit"-->
        <!--          ></el-image>-->
        <!--        </template>-->
        <!--        <template slot="select_icon" slot-scope="{ row }">-->
        <!--          <el-image-->
        <!--            v-if="row.select_icon == ''"-->
        <!--            style="width: 30px; height: 30px"-->
        <!--          >-->
        <!--            <div slot="error" class="image-slot">-->
        <!--              <i class="el-icon-picture-outline"></i>-->
        <!--            </div>-->
        <!--          </el-image>-->
        <!--          <el-image-->
        <!--            v-else-->
        <!--            style="width: 30px; height: 30px"-->
        <!--            :src="row.select_icon"-->
        <!--            :preview-src-list="[row.select_icon]"-->
        <!--            fit="fit"-->
        <!--          ></el-image>-->
        <!--        </template>-->
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit :data="current" :visible.sync="showEdit" @done="reload" />

    <!-- 榜单游戏列表展示 -->
    <GameList
      @gameResourcePositionEdit="gameResourcePositionEditRow"
      ref="gameResourcePosition"
      :data="current"
      :visible.sync="showPindao"
      @done="reload"
    />
    <!-- 编辑编辑频道游戏编辑弹窗 -->
    <GameListEdit
      :visible.sync="showResourceEdit"
      :data="currentEdit"
      @done="gameResourcePositionOver"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import edit from "./edit";
import GameList from "./game/index";
import GameListEdit from "./game/game-edit";

export default {
  name: "gameRanking",
  components: { edit, GameList, GameListEdit },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: "/rank.BoxGameRanking/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          minWidth: 45,
          align: "center",
        },
        {
          prop: "id",
          label: "ID",
          minWidth: 60,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "name",
          label: "名称",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
        },
        // {
        //   prop: "icon",
        //   label: "ICON",
        //   align: "center",
        //   minWidth: 80,
        //   slot: "icon",
        // },
        // {
        //   prop: "select_icon",
        //   label: "选中ICON",
        //   align: "center",
        //   minWidth: 80,
        //   slot: "select_icon",
        // },
        {
          prop: "device",
          label: "投放设备",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "device",
        },
        // {
        //   prop: "scene",
        //   label: "展示场景",
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: "center",
        //   slot: "scene",
        // },
        {
          prop: "sort",
          label: "排序",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 80,
        },
        {
          prop: "status",
          label: "状态",
          minWidth: 80,
          align: "center",
          slot: "status",
        },
        {
          prop: "create_time",
          label: "添加时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center",
        },
        {
          prop: "update_time",
          label: "更新时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center",
        },
        {
          columnKey: "action",
          label: "操作",
          minWidth: 160,
          align: "center",
          resizable: false,
          slot: "action",
          // fixed: "right"
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,

      //频道去是否显示弹窗
      showPindao: false,

      //游戏列表编辑管理编辑
      showResourceEdit: false,

      //当前游戏编辑数据
      currentEdit: null,

      showResource: false,
    };
  },

  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    // 打开频道区游戏列表
    openPindao(row) {
      this.current = row;
      this.showPindao = true;
    },

    /* 编辑资源位弹窗 */
    gameResourcePositionEditRow(row) {
      console.log(row);
      this.currentEdit = row;
      this.showResourceEdit = true;
    },
    gameResourcePositionOver() {
      this.$refs["gameResourcePosition"].reload();
    },

    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/rank.BoxGameRanking/delete", { id: row.id })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的数据吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/rank.BoxGameRanking/delete", {
              id: this.selection.map((d) => d.id),
            })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
        is_time: true,
      });
      this.$http
        .post("/rank.BoxGameRanking/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style scoped></style>
